﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace SanctusSoftware.Music.Lyrics
{
    public class StringCollection : IEnumerable, IEnumerable<string>, ICollection<string>
    {

        #region Constructor

        public StringCollection()
        {
            list = new List<string>();
        }

        #endregion

        List<string> list = null;
        
        
        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return ((IEnumerable)list).GetEnumerator();
        }

        #endregion

        #region IEnumerable<string> Members

        IEnumerator<string> IEnumerable<string>.GetEnumerator()
        {
            return list.GetEnumerator();
        }

        #endregion


        #region ICollection<string> Members

        public void Add(string item)
        {

            if (item == null || item.Trim().Length == 0)
            {
                throw new ArgumentNullException("Cannot add a blank item to the list");
            }

            if (this.Contains(item.Trim()))
            {
                throw new ArgumentException("The list already contains the item '" + item + "'");
            }

            list.Add(item.Trim());

        }

        public void Clear()
        {
            list.Clear();
        }

        public bool Contains(string item)
        {
            return list.Contains(item);
        }

        public void CopyTo(string[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return list.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(string item)
        {
            return list.Remove(item);
        }

        #endregion

        public string GetDelimitedValues()
        {
            return GetDelimitedValues("; ");
        }

        public string GetDelimitedValues(string delimiter)
        {

            StringBuilder sb = new StringBuilder();

            foreach (string value in list)
            {
                if (sb.Length > 0)
                {
                    sb.Append(delimiter);
                }
                sb.Append(value);
            }

            return sb.ToString();

        }

        public void AddRange(string delimitedList, string delimiter)
        {
            string[] values = delimitedList.Split(new string[] { delimiter },  StringSplitOptions.RemoveEmptyEntries);

            foreach (string value in values)
            {
                if (!this.Contains(value.Trim()))
                {
                    this.Add(value.Trim());
                }
            }

        }


    }
}
